<template>
  <div class="pageWrapper">
    <!-- 顶部 -->
    <el-card>
      <el-row type="flex">
        <el-col>
          <el-button size="small" type="primary" @click="isShowDialog = true">新建房产</el-button>
        </el-col>
        <el-col>
          <el-row type="flex">
            <el-select v-model="selectAgent" placeholder="经纪">
              <el-option :label="'全部经纪'" :value="''" />
              <el-option v-for="item in agentList" :key="item.id" :label="item.name" :value="item.id" />
            </el-select>
            <el-select v-model="selectCate" placeholder="分类">
              <el-option :label="'全部分类'" :value="''" />
              <el-option v-for="item in cateList" :key="item.id" :value="item.id" :label="item.title" />
            </el-select>
            <el-input v-model="keyword" />
            <el-button size="small" @click="search">搜索</el-button>
          </el-row>
        </el-col>
      </el-row>
    </el-card>
    <!-- 主要内容 -->
    <el-card class="main">
      <el-table border :data="list">
        <el-table-column label="封面" width="200px">
          <template v-slot="{row}">
            <img class="cover" :src="row.image" alt="">
          </template>
        </el-table-column>
        <el-table-column label="标题" prop="title" />
        <el-table-column label="标签">
          <template v-slot="{row}">
            <el-tag v-for="(item, index) in row.tags" :key="index" type="success">
              {{ item }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="地址" prop="address" />
        <el-table-column label="分类" prop="category.title" />
        <el-table-column label="经纪" prop="agent.name" />
        <el-table-column label="价格" prop="price" />
        <el-table-column label="介绍" prop="desc" />
        <el-table-column label="图片">
          <template v-slot="{row}">
            <img v-for="(item, index) in row.photos" :key="index" class="thumb" :src="item" alt="">
          </template>
        </el-table-column>
        <el-table-column label="操作" width="160px">
          <template v-slot="{row}">
            <el-button size="small" @click="showEdit(row.id)">编辑</el-button>
            <el-button size="small">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-row type="flex" justify="center" class="paginationWrapper">
        <el-pagination :current-page="currentPage" :total="total" :page-size="pageConfig._limit" @current-change="currentChange" />
      </el-row>
    </el-card>
    <!-- 弹窗 -->
    <AddProperty ref="addDialog" :is-show-dialog="isShowDialog" :agent-list="agentList" :cate-list="cateList" @closeDialog="isShowDialog = false" @reloadList="getList" />
  </div>
</template>

<script>
import {
  getPropertyList,
  getPropertyNumber,
  getAgentList,
  getCateList,
  getPropertyById
} from '@/api/property'
import AddProperty from './components/add-property.vue'
export default {
  components: {
    AddProperty
  },
  data() {
    return {
      selectAgent: '',
      selectCate: '',
      keyword: '',
      list: [],
      agentList: [],
      cateList: [],
      total: 0,
      currentPage: 1,
      pageConfig: {
        _limit: 2,
        _start: 0
      },
      isShowDialog: false
    }
  },
  created() {
    this.getList()
    this.getAgent()
    this.getCate()
  },
  methods: {
    async showEdit(id) {
      console.log(id)
      const res = await getPropertyById(id)
      this.$refs.addDialog.formData = res
      this.$refs.addDialog.formData.category = res.category.id
      this.$refs.addDialog.formData.agent = res.agent.id
      // 处理省市区
      const map = res.address.split('-')[0].split(' ')
      console.log('map', map)
      this.$refs.addDialog.map = map
      this.$refs.addDialog.formData.address = res.address.split('-')[1]
      this.isShowDialog = true
      // 给孙子组件传递数据
      this.$nextTick(() => {
        if (res.image) {
          this.$refs.addDialog.$refs.image.fileList = [{ url: res.image }]
        }
        if (res.photos && res.photos.length > 0) {
          this.$refs.addDialog.$refs.photos.fileList = res.photos.map(str => {
            return { url: str }
          })
        }
      })
    },
    async getList() {
      this.list = await getPropertyList({
        ...this.pageConfig,
        // 对于不想发出的数据, 给个 null 即可
        agent: this.selectAgent || null,
        category: this.selectCate || null,
        title_contains: this.keyword || null
      })
      this.total = await getPropertyNumber(
        // 拿数量一样带上筛选条件(拿到符合条件的总数量)
        {
          agent: this.selectAgent || null,
          category: this.selectCate || null,
          title_contains: this.keyword || null
        }
      )
    },
    async getAgent() {
      this.agentList = await getAgentList({
        _limit: 999,
        _start: 0
      })
    },
    async getCate() {
      this.cateList = await getCateList({
        _limit: 999,
        _start: 0
      })
    },
    currentChange(newPage) {
      this.currentPage = newPage
      this.pageConfig._start = (newPage - 1) * this.pageConfig._limit
      this.getList()
    },
    search() {
      // 先翻到第一页然后搜索
      this.pageConfig._start = 0
      this.currentPage = 1
      this.getList()
    }
  }
}
</script>

<style lang="scss" scoped>
.pageWrapper {
  padding: 20px;
}
.main {
  margin-top: 20px;
}
.paginationWrapper {
  padding: 20px;
}
.thumb {
  width: 80px;
  height: 80px;
}
.cover {
  width: 180px;
  height: 180px;
}
</style>
